<?php
namespace Admin\Model;
use Think\Model;

class MonthreportModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('ProjectID','number','请选择项目名称',Model::MUST_VALIDATE,'function',1),
        array('ReportMonth','require','请选报告月份',Model::MUST_VALIDATE,'function',1),
        array('Status','number','请选择项目进行状态',Model::MUST_VALIDATE,'function',1)
    );
	
	protected $_auto = array(
    	array('UserID','getUserID',1,'callback')
  	);
  	
  	public function getUserID(){
		return session('USER_ID');
	}
	
	public function Myupadd(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->add();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "添加成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "添加错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myshow($parmArr){
		return $this->field("*")->WHERE($parmArr)->find();
	}
	
	public function MyshowList(){
		$where = "";
		$inid = "";
		$keyword = I('keyword');
		/*
        if(!empty($keyword)){
			$res = M("project")->field("ID")->where(array("Name"=>array('like','%'.$keyword.'%')))->select();
			foreach($res AS $k => $v){
				$inid .= $v['ID'].",";
			}
			if(!empty($inid)){
				$where['ProjectID'] = array('IN',substr($inid, 0, -1));
			}else{
				$where['ProjectID'] = "-1";
			}
		}
		*/
		if(!empty($keyword)){
			$res = M("datadict")->field("ID")->where(array("Name"=>array('like','%'.$keyword.'%')))->select();
			foreach($res AS $k => $v){
				$inid .= $v['ID'].",";
			}
			if(!empty($inid)){
				$where['a.Status'] = array('IN',substr($inid, 0, -1));
			}else{
				$where['a.Status'] = "-1";
			}
		}
		$id = (int)I('id');
		if(!empty($id)){
			$where['ProjectID'] = $id; 
		}
		$return = array();
		$count = $this->alias('a')->where($where)->count();
	    $page = $this->Mypage($count, 8);
	    $fields = " a.*, b.Name AS ProjectName, c.Name AS StatusName ";
	    $joina = " LEFT JOIN project AS b ON a.ProjectID = b.ID ";
	    $joinb = " LEFT JOIN datadict AS c ON a.Status = c.ID";
		$res = $this->alias('a')->join($joina)->join($joinb)->where($where)->field($fields)->limit($page->firstRow.','.$page->listRows)->select();
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	
	public function getProjectList(){
		$depat = M("project");
		//$where['IsDel'] = 0;
		return $depat->field('ID,Name')->where()->select();
	}
	
	public function getStatusList(){
		$db = M("datadict");
		return $db->alias('a')->field('b.*')->join(" LEFT JOIN datadict AS b ON a.ID = b.ParentID")->where(array("a.Name" => "项目进行状态","a.IsDel" => 0))->select();
	}
	
	public function getTableData(){
		$member = ' ';
		$dataArr = array();
		$join1 = " LEFT JOIN project AS b ON a.ProjectID = b.ID";
		$res1 = $this->alias('a')->field('a.*,b.StartDate,b.EndDate,b.Name AS projectName,(SELECT UserName FROM user WHERE ID = b.ChargeUserID) AS ChargeUserName')->join($join1)->where(array("a.ProjectID"=>$_GET['id']))->select();
		$res2 = M("project_user_rel as a")->field("b.UserName")->join("LEFT JOIN user AS b ON a.UserID = b.ID")->where(array("a.ProjectID"=>$_GET['id']))->select();
		if(!empty($res2)){
			foreach($res2 AS $k => $v){
				$member .= $v['UserName'].',';
			}
		}
		foreach($res1 AS $k => $v){
			$res1[$k]['team'] = substr($member,0,-1);
		}
		$dataArr['data'] = $res1;
		return $dataArr;
		//var_dump($res1);
	}	
}

?>				